package com.dingwen.treasure.base.enums;

import cn.hutool.core.util.StrUtil;
import com.dingwen.treasure.base.service.DesensitizationService;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Optional;

/**
 * 　DesensitizationFunction: 数据脱敏方法枚举
 * 　@author dingwen
 * 　@date 2022/7/19
 */
@Getter
@AllArgsConstructor
public enum DesensitizationFunction {

    /**
     * 不进行加密
     */
    NO_MASK((content, mask) -> content),

    /**
     * 只显示前四位后四位
     */
    EXCEPT_BEFORE_4_AFTER_4_MASK(((content, mask) -> {
        String nowMask = Optional.ofNullable(mask).orElse(DesensitizationService.MASK);
        if (StrUtil.isEmpty(content)) {
            return content;
        }
        StringBuilder stringBuilder = new StringBuilder(content);
        String tempMask = "";
        for (int i = 0; i < content.length()-8; i++) {
            tempMask =  tempMask.concat(nowMask);
        }
        return stringBuilder.replace(5, content.length() - 4, tempMask).toString();
    })),

    /**
     * 完整加密
     */
    ALL_MASK((content, mask) -> {
        String nowMask = Optional.ofNullable(mask).orElse(DesensitizationService.MASK);
        if (StrUtil.isEmpty(content)) {
            return content;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 1; i <= content.length(); i++) {
            stringBuilder.append(nowMask);
        }
        return stringBuilder.toString();
    });


    /**
     * 加密服务
     */
    private DesensitizationService desensitizationService;

}
